/*******************************************************************************
 * Copyright (c) 2000, 2003 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package com.anis.project.medcare.dicom.viewer.view;

import java.awt.Container;
import java.awt.Frame;
import java.awt.Transparency;
import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.ConvolveOp;
import java.awt.image.DataBuffer;
import java.awt.image.DataBufferUShort;
import java.awt.image.DirectColorModel;
import java.awt.image.IndexColorModel;
import java.awt.image.Kernel;
import java.awt.image.Raster;
import java.awt.image.SampleModel;
import java.awt.image.WritableRaster;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.spi.ImageWriterSpi;
import javax.imageio.stream.ImageInputStream;
import javax.media.jai.PlanarImage;

import org.dcm4che2.data.DicomObject;
import org.dcm4che2.data.Tag;
import org.dcm4che2.imageio.plugins.dcm.DicomImageReadParam;
import org.dcm4che2.imageio.plugins.dcm.DicomStreamMetaData;
import org.dcm4che2.imageioimpl.plugins.dcm.DicomImageWriter;
import org.dcm4che2.io.DicomOutputStream;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;

public class DicomImageHandler {
	SWTImageCanvas imageCanvas;
	private List<File> sourceFiles;

	public DicomImageHandler(List<File> dicomFile) {
		this.sourceFiles = dicomFile;
	}

	public ArrayList<ImageData> createDicomImage() {
		Iterator iter = ImageIO.getImageReadersByFormatName("DICOM");
		ImageReader reader = (ImageReader) iter.next();
		DicomImageReadParam param = (DicomImageReadParam) reader
				.getDefaultReadParam();
		ArrayList<ImageData> listOfImages = new ArrayList<ImageData>();
		Iterator fileIter = sourceFiles.iterator();
		while (fileIter.hasNext()) {
			File file = (File) fileIter.next();
			ImageData dicomSlot = createImage(file, reader, param);
			listOfImages.add(dicomSlot);
		}
		return listOfImages;
	}

	private ImageData createImage(File file, ImageReader reader,
			DicomImageReadParam param) {
		ImageData imageData = null;
		try {
			ImageInputStream iis = ImageIO.createImageInputStream(file);
			reader.setInput(iis, false);
			Raster raster = reader.readRaster(0, param);

			if (raster == null) {
				System.out.println("Error: couldn't read Dicom image!");
				return null;
			}
			iis.close();
			float[] emboss = new float[] { -2, 0, 0, 0, 1, 0, 0, 0, 2 };
			Kernel kernel = new Kernel(3, 3, emboss);
			ConvolveOp op = new ConvolveOp(kernel);
			Raster newRaster = op.filter(raster, null);
			DataBufferUShort buffer = (DataBufferUShort) newRaster
					.getDataBuffer();
			short[] pixelData = buffer.getData();

			WritableRaster writableRaster = newRaster
					.createCompatibleWritableRaster();
			writableRaster.setDataElements(0, 0, newRaster);

			DicomStreamMetaData dsmd = (DicomStreamMetaData) reader
					.getStreamMetadata();
			DicomObject dicom = dsmd.getDicomObject();

			dicom.putShorts(Tag.PixelData, dicom.vrOf(Tag.PixelData), pixelData);

			byte[] dicomByteArray = toByteArray(dicom);
			BufferedImage bm = getPixelDataAsBufferedImage(dicomByteArray);

			imageData = SWT2Dutil.convertToSWT(bm);
		} catch (IOException e) {
			System.out.print("Convertion Failure");
			e.printStackTrace();
		}
		return imageData;
	}

	private BufferedImage getPixelDataAsBufferedImage(byte[] dicomData)
			throws IOException {
		ByteArrayInputStream bais = new ByteArrayInputStream(dicomData);
		BufferedImage buff = null;
		Iterator<ImageReader> iter = ImageIO
				.getImageReadersByFormatName("DICOM");
		ImageReader reader = (ImageReader) iter.next();
		DicomImageReadParam param = (DicomImageReadParam) reader
				.getDefaultReadParam();
		ImageInputStream iis = ImageIO.createImageInputStream(bais);
		reader.setInput(iis, false);
		buff = reader.read(0, param);
		iis.close();
		if (buff == null)
			throw new IOException(
					"Could not read Dicom file. Maybe pixel data is invalid.");
		return buff;
	}

	private byte[] toByteArray(DicomObject obj) throws IOException {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		BufferedOutputStream bos = new BufferedOutputStream(baos);
		DicomOutputStream dos = new DicomOutputStream(bos);
		dos.writeDicomFile(obj);
		dos.close();
		byte[] data = baos.toByteArray();
		return data;
	}

}